package com.lantu.transcoder;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;

public class SaveAsJPEG {
	public static void main(String[] args ){
		String svg="<svg id=\"SvgjsSvg1006\" width=\"100%\" height=\"100%\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:svgjs=\"http://svgjs.com/svgjs\"><defs id=\"SvgjsDefs1007\"><marker id=\"SvgjsMarker1099\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#000000\"><line id=\"SvgjsLine1100\" x1=\"10\" y1=\"10\" x2=\"20\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine1101\" x1=\"10\" y1=\"10\" x2=\"20\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker><marker id=\"SvgjsMarker1102\" markerWidth=\"20\" markerHeight=\"20\" refX=\"10\" refY=\"10\" viewBox=\"0 0 20 20\" orient=\"auto\" fill=\"#000000\"><line id=\"SvgjsLine1103\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"0\" stroke=\"#000000\" stroke-width=\"1\"></line><line id=\"SvgjsLine1104\" x1=\"10\" y1=\"10\" x2=\"0\" y2=\"20\" stroke=\"#000000\" stroke-width=\"1\"></line></marker></defs><ellipse id=\"SvgjsEllipse1008\" rx=\"55\" ry=\"55\" cx=\"316\" cy=\"150\" fill=\"#ffffff\" fill-opacity=\"1\" stroke=\"#000000\" stroke-width=\"1\"></ellipse><ellipse id=\"SvgjsEllipse1020\" rx=\"52\" ry=\"52\" cx=\"581\" cy=\"139\" fill=\"#ffffff\" fill-opacity=\"1\" stroke=\"#000000\" stroke-width=\"1\" style=\"fill: rgb(0, 239, 0);\"></ellipse><path id=\"SvgjsPath1032\" d=\"M350.8592224121094 243.02801513671875Q450.8592224121094 293.02801513671875 550.8592224121094 243.02801513671875 \" fill=\"transparent\" style=\"stroke: black;\" marker-start=\"url(#SvgjsMarker1099)\" marker-end=\"url(#SvgjsMarker1102)\" stroke=\"#000000\" transform=\"matrix(-0.9996573249755577,-0.02617694830787335,0.02617694830787335,-0.9996573249755577,896.6885947473261,405.7382967070357)\"></path><text id=\"SvgjsText1149\" font-family=\"Helvetica\" x=\"308.9248046875\" y=\"88\" font-size=\"50\" text-anchor=\"middle\" style=\"fill: rgb(0, 0, 0);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan1239\" dy=\"75\" x=\"308.9248046875\" svgjs:data=\"{&quot;newLined&quot;:true}\">A</tspan></text><text id=\"SvgjsText1416\" font-family=\"Helvetica\" x=\"588.9873046875\" y=\"93\" font-size=\"50\" text-anchor=\"middle\" style=\"fill: rgb(0, 0, 0);\" svgjs:data=\"{&quot;leading&quot;:&quot;1.5em&quot;}\"><tspan id=\"SvgjsTspan1628\" dy=\"75\" x=\"588.9873046875\" svgjs:data=\"{&quot;newLined&quot;:true}\">B</tspan></text></svg>";
		try {
			
			SaveAsJPEG.svg2Jpeg(svg,"test.jpg");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void svg2Jpeg(String svg1,String filename) {
		String svg=svg1.replace("fill=\"transparent\"", "fill-opacity=\"0\" ");
        // Create a JPEG transcoder
        JPEGTranscoder t = new JPEGTranscoder();
        
        // Set the transcoding hints.
        t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
                   new Float(1));
        t.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, new Float(800));
        // Create the transcoder input.
        ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(svg.getBytes());
        //String svgURI = new File("test.svg").toURL().toString();
        TranscoderInput input = new TranscoderInput(tInputStringStream);
       
        // Create the transcoder output.
        OutputStream ostream=null;
		try {
			ostream = new FileOutputStream("d:\\test\\"+filename);
			TranscoderOutput output = new TranscoderOutput(ostream);

	        // Save the image.
	        t.transcode(input, output);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TranscoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			// Flush and close the stream.
	        try {
				ostream.flush();
				ostream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
		}
        

        
    }
}
